package com.github.yoojia.flow.impl;

import com.github.yoojia.flow.Context;
import com.github.yoojia.flow.Event;
import com.github.yoojia.flow.EventHandler;
import com.github.yoojia.flow.ScriptingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.script.ScriptException;

/**
 * JavaScript事件处理器
 * @author 陈小锅 (yoojiachen@gmail.com)
 * @since 1.0.0
 */
public class ScriptingEventHandler extends ScriptingContext implements EventHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptingEventHandler.class);

    @Override
    public void onHandleEvent(Context context, Event event) {
        try {
            invokeFunction("onHandleEvent", context, event);
        } catch (ScriptException e) {
            LOGGER.error("Error when invoke interceptor script", e);
        } catch (NoSuchMethodException e) {
            LOGGER.error("Script file, onHandleEvent(Context, Event) function not found", e);
        }
    }
}
